package com.study.zzedu.ui.exercise.adapter;

import java.util.List;

import com.study.zzedu.R;
import com.study.zzedu.ui.exercise.bean.QuestionOptionBean;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class OptionsListAdapter extends BaseAdapter {
    private Context mContext;
    ListView lv;
    int index;
    public List<QuestionOptionBean> options;

    public OptionsListAdapter(Context context, List<QuestionOptionBean> options, ListView lv, int index) {
        this.mContext = context;
        this.options = options;
        this.lv = lv;
        this.index = index;
    }

    public int getCount() {
        return options.size();
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {

        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View view = LayoutInflater.from(mContext).inflate(
                R.layout.list_item_option, null);
        //TextView ctv = (TextView) view.findViewById(R.id.ctv_name);
        LinearLayout llv = (LinearLayout) view.findViewById(R.id.ll_option);
        TextView option = (TextView) view.findViewById(R.id.tv_option);

        //ctv.setText(options.get(position).getName());
        option.setText(options.get(position).getName()+". "+options.get(position).getDescription());
        updateBackground(position, llv, option);
        return view;
    }

    public void updateBackground(int position, View view, TextView tv) {
        int backgroundId;
        if (lv.isItemChecked(position)) {
            backgroundId = R.drawable.exercise_option_bg2;
            tv.setTextColor(tv.getContext().getResources().getColor(R.color.bg_blue));
        } else {
            backgroundId = R.drawable.exercise_option_bg;
            tv.setTextColor(Color.BLACK);
        }
        Drawable background = mContext.getResources().getDrawable(backgroundId);
        view.setBackgroundDrawable(background);
    }

}
